define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method; 
        
    var $js_item_list = $("#js_item_list")
    $js_item_list.length && (function() {
        var $js_make_item = $("#js_make_item");
        var $js_add_item = $("#js_add_item"); 
        $js_add_item.on("click", function() {
            
            $("#js_item_modal").find("input[type=checkbox]").prop("checked",false);
                $(".dd-item",$js_item_list).each(function(){
                    var $this=$(this),val=$this.find("input[type=hidden]:first").val();
                    $("#js_item_modal").find("input[type=checkbox][value="+val+"]").prop("checked",true);
                });
            $("#js_item_modal").modal("show");

        });

        var done = function() {
            $js_item_list.nestable({
                maxDepth: 1
            });
            $('.dd-handle a').on('mousedown', function(e) {
                e.stopPropagation();
            });
        }
        require.async("nestable", done);
        $js_make_item.on("click", function() {
           
                $("#noitems:visible").addClass('hd');
                var $item = $('#js_item_modal input[name="item"]:checked');
                if($item.length<6){
                    $("ol", $js_item_list).html("");
                    $item.each(function(){
                        var add_item_length = $(".dd-item").length,$this=$(this);
                        var data = {
                            name: $this.data("name"),
                            val: $this.val(),
                            index: add_item_length,
                            id: add_item_length
                        }
                        $("ol", $js_item_list).append(template("js_item_html", data));
                    });
                    $("#js_item_modal").modal("hide");
                    done();
                }else{
                    config.msg.info(config.lang.maxNavLength)
                }


        });
        var $js_items = $("#js_items")
        $('button[type="submit"]').on("click", function() {
            $js_items.val(window.JSON.stringify($js_item_list.nestable('serialize')))
        })
    })();

 
})
